<?php

namespace App\Http\Controllers\Front;

use App\Http\Controllers\Controller;
use App\Http\Service\MessageService;
use App\Http\Service\SensitiveWordsService;
use App\Http\Service\VoteService;
use App\Model\Articles;
use App\Model\Comment;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Swagger\Annotations as SWG;

class CommentController extends Controller
{
    /**
     * @SWG\Get(
     *     path="/front/articles/{id}/comments",
     *     tags={"文章相关"},
     *     summary="评论列表",
     *     description="评论列表",
     *     produces={"application/json"},
     *     @SWG\Parameter(ref="#/parameters/auth"),
     *     @SWG\Parameter(name="id", in="path", type="integer", description="文章id"),
     *     @SWG\Response(
     *          response="200",
     *          description="调用成功",
     *          @SWG\Schema(
     *              type="array",
     *              @SWG\Items(
     *                  @SWG\Property(property="id", type="integer", description="id"),
     *                  @SWG\Property(
     *                      property="user",
     *                      description="用户信息",
     *                      @SWG\Property(property="id", type="integer", description="用户id"),
     *                      @SWG\Property(property="name", type="string", description="用户"),
     *                      @SWG\Property(property="portrait", type="string", description="用户头像"),
     *                      @SWG\Property(property="level", type="integer", description="vip等级"),
     *                      @SWG\Property(property="level_icon", type="string", description="vip图标"),
     *                  ),
     *                  @SWG\Property(property="content", type="string", description="评论内容"),
     *                  @SWG\Property(property="thumb", type="intenger", description="点赞数"),
     *                  @SWG\Property(property="created_at", type="string", format="date", description="评论时间"),
     *                  @SWG\Property(property="is_thumb", type="integer", description="是否点赞"),
     *              )
     *          )
     *      )
     * )
     */
    public function index($id, Comment $comment)
    {
        return $this->renderJson($comment->listByArticle($id));
    }

    /**
     * @SWG\Post(
     *     path="/front/articles/{id}/comments",
     *     tags={"文章相关"},
     *     summary="评论",
     *     description="评论",
     *     consumes={"application/x-www-form-urlencoded"},
     *     produces={"application/json"},
     *     @SWG\Parameter(ref="#/parameters/auth"),
     *     @SWG\Parameter(name="id", in="path", required=true, type="integer", description="文章id"),
     *     @SWG\Parameter(name="content", in="formData", required=true, type="string", description="内容"),
     *     @SWG\Response(
     *          response="200",
     *          description="操作成功",
     *          @SWG\Schema(
     *              @SWG\Property(property="id", type="integer", description="id"),
     *                  @SWG\Property(
     *                      property="user",
     *                      description="用户信息",
     *                      @SWG\Property(property="id", type="integer", description="用户id"),
     *                      @SWG\Property(property="name", type="string", description="用户"),
     *                      @SWG\Property(property="portrait", type="string", description="用户头像"),
     *                      @SWG\Property(property="level", type="integer", description="vip等级"),
     *                      @SWG\Property(property="level_icon", type="string", description="vip图标"),
     *                  ),
     *               @SWG\Property(property="content", type="string", description="评论内容"),
     *               @SWG\Property(property="thumb", type="integer", description="点赞数"),
     *               @SWG\Property(property="created_at", type="string", format="date", description="评论时间"),
     *               @SWG\Property(property="is_thumb", type="integer", description="是否点赞"),
     *          )
     *      )
     * )
     */
    public function store(Request $request, $id, Comment $comment)
    {
        $this->validate($request, [
            'content' => 'required'
        ],[
            'content.required' => '内容不能为空'
        ]);
        $comment->a_id = $id;
        $comment->user_id = Auth::id();
        $util = new SensitiveWordsService();
        $comment->content = $util->filter(request('content'));
        $article = Articles::find($id);
        $article->increment('comment_num');
        $article->last_comment_at = date('Y-m-d H:i:s');
        //发送消息
        if ($comment->save()) {
            $article->save();
            MessageService::message(Auth::id(), $article->author, 3, $comment->id);
            $res = [
                'id' => $comment->id,
                'content' => $comment->content,
                'thumb' => 0,
                'is_thumb' => false,
                'created_at' => $comment->created_at->format('Y-m-d H:i:s'),
                'user' => [
                    'id' => Auth::id(),
                    'level' => User::is_vip(Auth::id()),
                    'level_icon' => '',
                    'name' => Auth::user()->name(),
                    'portrait' => Auth::user()->portrait()
                ]
            ];
            return $this->renderJson($res);
        }
        return $this->error('评论失败');
    }

    /**
     * @SWG\Post(
     *     path="/front/thumb/comment/{id}",
     *     tags={"文章相关"},
     *     summary="评论点赞",
     *     description="评论点赞",
     *     produces={"application/json"},
     *     @SWG\Parameter(ref="#/parameters/auth"),
     *     @SWG\Parameter(name="id", in="path", required=true, type="integer", description="评论id"),
     *     @SWG\Response(
     *          response="200",
     *          description="取消点赞成功"
     *      )
     * )
     */
    public function thumb($id)
    {
        try {
            VoteService::voteUp(2, $id);
        } catch (\Exception $e) {
            return $this->fail($e->getMessage());
        }
        return $this->success();
    }

    /**
     * @SWG\Post(
     *     path="/front/un_thumb/comment/{id}",
     *     tags={"文章相关"},
     *     summary="取消评论点赞",
     *     description="取消评论点赞",
     *     produces={"application/json"},
     *     @SWG\Parameter(ref="#/parameters/auth"),
     *     @SWG\Parameter(name="id", in="path", required=true, type="integer", description="评论id"),
     *     @SWG\Response(
     *          response="200",
     *          description="取消点赞成功"
     *      )
     * )
     */
    public function unThumb($id)
    {
        try {
            VoteService::unVoteUp(2, $id);
        } catch (\Exception $e) {
            return $this->fail($e->getMessage());
        }
        return $this->success();
    }
}
